package com.sinosoft.perfect.common.xmlService;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class XmlInfoBean implements Serializable {
	
	private static final long serialVersionUID = 1L;

	private String name;
	
	private String content;
	
	private Map<String,String> properties;
	//0 is root.
	private String level;
	
	private String myid;
	
	private String parentId;
	
	public String getLevel() {
		return level;
	}

	public void setLevel(String level) {
		this.level = level;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getMyid() {
		return myid;
	}

	public void setMyid(String myid) {
		this.myid = myid;
	}

	public String getParentId() {
		return parentId;
	}

	public void setParentId(String parentId) {
		this.parentId = parentId;
	}

	public Map<String, String> getProperties() {
		return properties;
	}

	public void setProperties(Map<String, String> properties) {
		this.properties = properties;
	}
	
	@SuppressWarnings("unchecked")
	public static XmlInfoBean copy(Object o) throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, InstantiationException{
		XmlInfoBean x = new XmlInfoBean();
		Class<?> c = o.getClass();
		Field[] fs = c.getDeclaredFields();
		for(int i=0;i<fs.length;i++){
			Field f = fs[i];
			String fname = f.getName();
			if(fname .equals("serialVersionUID")){continue;}
			Object value = getValueFromGetMethodNameByClassAndObject(fname,c,o);
			if(value == null )continue;
			if(fname .equals("name")){
				x.setName(""+value);
			}else if(fname .equals("content")){
				x.setContent(""+value);
			}else if(fname .equals("properties")){
				Map<String,String> properties = (Map<String,String>)value;
				x.setProperties(properties);
			}else if(fname .equals("level")){
				x.setLevel(""+value);
			}else if(fname.equals("myid")){
				x.setMyid(""+value);
			}else if(fname .equals("parentId")){
				x.setParentId(""+value);
			}else{
				Map<String,String> proMap = x.getProperties();
				if(proMap == null) proMap = new HashMap<String,String>();
				proMap.put(fname, ""+value);
				x.setProperties(proMap);
			}
		}
		return x;
	}
	private static Object getValueFromGetMethodNameByClassAndObject(String fname,Class<?> c,Object o)
		throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
		String getMethodName = composeGetMethodNameByFieldName(fname);
		Method m = c.getMethod(getMethodName);
		Object [] pos = new Object[0];
		Object value = m.invoke(o, pos);
		return value;
	}
	
	private static String composeGetMethodNameByFieldName(String fieldName){
		String subFnameWord = fieldName.substring(0, 1);
		String getMethodName = "get"+fieldName.replaceFirst(subFnameWord, subFnameWord.toUpperCase());
		return getMethodName;
	}
	
}
